
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <thread>
#include <mutex>

using namespace std;

// 全局共享变量
int counter = 0;

mutex mtx;

void increment()
{
    for (int i = 0; i < 100000; ++i)
    {
        mtx.lock();
        counter = counter + 1; // 数据竞争！
        mtx.unlock();
    }
}

int main()
{
    std::thread t1(increment);
    std::thread t2(increment);

    t1.join();
    t2.join();

    // 结果不确定，可能小于200000
    std::cout << "Final counter: " << counter << std::endl;
    return 0;
}